package com.ruoyi.web.controller.module.asset;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.asset.EstateAsset;
import com.ruoyi.system.domain.module.asset.EstateAssetCheckRecord;
import com.ruoyi.system.domain.module.asset.EstateAssetInfo;
import com.ruoyi.system.service.module.asset.IEstateAssetCheckRecordService;
import com.ruoyi.system.service.module.asset.IEstateAssetInfoService;
import com.ruoyi.system.service.module.asset.IEstateAssetService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 资产盘点记录Controller
 *
 * @author ruoyi
 * @date 2025-01-24
 */
@Validated
@Api(value = "资产盘点记录控制器", tags = {"资产盘点记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/checkRecord")
public class EstateAssetCheckRecordController extends BaseController {

    private final IEstateAssetCheckRecordService estateAssetCheckRecordService;
	private final IEstateService estateService;
	private final IWxUserService wxUserService;
	private final ISysUserService sysUserService;
	private final IEstateAssetService estateAssetService;
	private final IEstateAssetInfoService estateAssetInfoService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;
    /**
     * 查询资产盘点记录列表
     */
    @ApiOperation("查询资产盘点记录列表")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:list')")
    @GetMapping("/list")
    public List<EstateAssetCheckRecord> list(@Validated EstateAssetCheckRecord estateAssetCheckRecord) {
		List<EstateAssetCheckRecord> checkRecordList = new ArrayList<>();
		if (estateAssetCheckRecord.getPlanId()!=null){
			QueryWrapper<EstateAssetInfo> qwInfo = new QueryWrapper<>();
			qwInfo.eq("plan_id",estateAssetCheckRecord.getPlanId());
			List<EstateAssetInfo> infoList = estateAssetInfoService.list(qwInfo);
			for (EstateAssetInfo info:infoList){
				QueryWrapper<EstateAssetCheckRecord> qw = new QueryWrapper<>();
				if(info.getAssetPointId()!=null){
					qw.eq("asset_id",info.getAssetPointId());
				}
				List<EstateAssetCheckRecord> list = estateAssetCheckRecordService.list(qw);
				if (list.size()>0){
					for (EstateAssetCheckRecord list1:list){
						if (list1.getEstateId()!=null){
							list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
						}
						if (list1.getAssetId()!=null){
							list1.setAssetName(estateAssetService.findById(list1.getAssetId()).getAssetName());
						}
						if (list1.getImgUrl()!=null){
							list1.setImgUrl(imgUrl + list1.getImgUrl());
						}
						checkRecordList.add(list1);
					}
				}
			}
		}else {
			QueryWrapper<EstateAssetCheckRecord> qw = new QueryWrapper<>();
			if(estateAssetCheckRecord.getAssetId()!=null){
				qw.eq("asset_id",estateAssetCheckRecord.getAssetId());
			}
			TableDataInfo<EstateAssetCheckRecord> td = PageUtils.buildDataInfo(estateAssetCheckRecordService.page(PageUtils.buildPage(),qw));
			List<EstateAssetCheckRecord> list = td.getRows();
			if (list.size()>0){
				for (EstateAssetCheckRecord list1:list){
					if (list1.getEstateId()!=null){
						list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
					}
					if (list1.getAssetId()!=null){
						list1.setAssetName(estateAssetService.findById(list1.getAssetId()).getAssetName());
					}
					if (list1.getImgUrl()!=null){
						list1.setImgUrl(imgUrl + list1.getImgUrl());
					}
					checkRecordList.add(list1);
				}
			}
		}
        return checkRecordList;
    }

    /**
     * 导出资产盘点记录列表
     */
    @ApiOperation("导出资产盘点记录列表")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:export')")
    @Log(title = "资产盘点记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateAssetCheckRecord> export(@Validated EstateAssetCheckRecord estateAssetCheckRecord) {
        List<EstateAssetCheckRecord> list = estateAssetCheckRecordService.list();
        ExcelUtil<EstateAssetCheckRecord> util = new ExcelUtil<EstateAssetCheckRecord>(EstateAssetCheckRecord.class);
        return util.exportExcel(list, "资产盘点记录");
    }

    /**
     * 获取资产盘点记录详细信息
     */
    @ApiOperation("获取资产盘点记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateAssetCheckRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateAssetCheckRecord estateAssetCheckRecord = estateAssetCheckRecordService.getById(id);
        return AjaxResult.success(estateAssetCheckRecord);
    }

    /**
     * 新增资产盘点记录
     */
    @ApiOperation("新增资产盘点记录")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:add')")
    @Log(title = "资产盘点记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateAssetCheckRecord estateAssetCheckRecord) {
        return toAjax(estateAssetCheckRecordService.save(estateAssetCheckRecord) ? 1 : 0);
    }

    /**
     * 修改资产盘点记录
     */
    @ApiOperation("修改资产盘点记录")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:edit')")
    @Log(title = "资产盘点记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateAssetCheckRecord estateAssetCheckRecord) {
        return toAjax(estateAssetCheckRecordService.updateById(estateAssetCheckRecord) ? 1 : 0);
    }

    /**
     * 删除资产盘点记录
     */
    @ApiOperation("删除资产盘点记录")
    @PreAuthorize("@ss.hasPermi('module:checkRecord:remove')")
    @Log(title = "资产盘点记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateAssetCheckRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
